/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BD;

import BD.HibernateUtil;
import BE.TipoDocumentoBE;
import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Xatuuu
 */
public class TipoDocumentoBD {

    private Session session = null;

    public TipoDocumentoBD() {

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();

    }

    public boolean eliminarTipoDocumento(TipoDocumentoBE objTipodocumento) {

        try {
            Transaction tx = session.beginTransaction();
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = (Transaction) session.beginTransaction();
            session.flush();
            session.evict(objTipodocumento);
            session.update(objTipodocumento);
            tx.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean insertarTipoDocumento(TipoDocumentoBE objTipodocumentoBE) {

        try {

            Transaction tx = session.beginTransaction();
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = (Transaction) session.beginTransaction();
            session.save(objTipodocumentoBE);
            tx.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean modificarTipoDocumento(TipoDocumentoBE objTipodocumento) {

        try {

            Transaction tx = session.beginTransaction();
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = (Transaction) session.beginTransaction();
            session.flush();
            session.evict(objTipodocumento);

            TipoDocumentoBE objTipoDocumentoX = (TipoDocumentoBE) session.get(TipoDocumentoBE.class, new Integer(objTipodocumento.getSrlIdtipodocumento()));

            if (objTipoDocumentoX == null) {

                objTipoDocumentoX = new TipoDocumentoBE();
                objTipoDocumentoX.setCvNombredocumento(objTipodocumento.getCvNombredocumento());
                objTipoDocumentoX.setBoolEliminado(objTipodocumento.getBoolEliminado());

            }
            objTipoDocumentoX.setCvNombredocumento(objTipodocumento.getCvNombredocumento());
            objTipoDocumentoX.setBoolEliminado(objTipodocumento.getBoolEliminado());
            session.saveOrUpdate(objTipoDocumentoX);
            tx.commit();
            //session.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Collection seleccionarTipoDocumento() {
        Transaction tx = session.beginTransaction();
        Criteria crit = session.createCriteria(TipoDocumentoBE.class);
        return crit.list();
    }

    public Collection buscarTipoDocumento(TipoDocumentoBE objTipodocumento) {

        List<TipoDocumentoBE> lista = null;
        try {
            Transaction tx = session.beginTransaction();
            Criteria crit = session.createCriteria(TipoDocumentoBE.class);

            if (objTipodocumento.getSrlIdtipodocumento() > 0) {
                crit.add(Restrictions.eq("srlIdtipodocumento", objTipodocumento.getSrlIdtipodocumento()));
            }
            if (objTipodocumento.getCvNombredocumento() != null) {
                crit.add(Restrictions.like("cvNombredocumento", objTipodocumento.getCvNombredocumento()));
            }
            if (objTipodocumento.getBoolEliminado() != null) {
                crit.add(Restrictions.like("boolEliminado", objTipodocumento.getBoolEliminado()));
            }

            session.flush();
            lista = crit.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }
}
